home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / glammar / gg07.c < prev    next >
C/C++ Source or Header  |  1991-01-21  |  16KB  |  563 lines

  1. /*
  2.  
  3.     This file is a part of the GLAMMAR source distribution 
  4.     and therefore subjected to the copy notice below. 
  5.     
  6.     Copyright (C) 1989,1990  Eric Voss, ericv@cs.kun.nl 
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation version 1
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /* file: lattices */
  22. #include "gg1.h"
  23. #include "gg2.h"
  24.  
  25. int group = 0, count,top, unique_lattice_count=0, prev_group = -1;
  26. char  repr_group[1000][6], *lattice_substitution_name,*lattice_val_repr;
  27. int max_group,prev_mem;
  28. static int lline;
  29. static int lat_error = 0;
  30.  
  31. set_lattice_groups() {
  32.   int ag;
  33.   for (ag = first_lattice; ag != nil; ag = BROTHER(ag)) {
  34.     if (DEF(ag) == -1) {
  35.       count = 0;
  36.       DEF(ag) = group;
  37.       top = ag;
  38.       (void) sprintf(repr_group[group],"%d",group);
  39.       set_group(ag);
  40.       group += 1;
  41.     }
  42.   }
  43.   max_group = group;
  44. /*  fprintf (stderr,"%d lattice catogories\n",group); */
  45. }
  46.  
  47.  
  48. set_group(ag)
  49. int ag;
  50. {
  51.   int mem = SON(ag);
  52.   if (REPR(mem) == REPR(ag)) {
  53.       NODENAME(mem) =  1 << count++;
  54.       NODENAME(ag) =  NODENAME(mem);
  55.       DEF(mem) =  -1;
  56.       if (count > 30) {
  57.         fprintf(stderr, "lattice rule %s: to much elements (>31)", REPR(ag));
  58.         exit(1);
  59.       }
  60.      return;
  61.   } 
  62.   for (mem = SON(ag) ; mem != nil; mem = BROTHER(mem)) {
  63.     int def  = DEF(mem);
  64.     if ( def == -1) 
  65.        set_mem_lattice(mem);
  66.     else if (DEF(def) == -1) {
  67.       DEF(def) = group;
  68.       set_group(def);
  69.     }
  70.     else if (DEF(def) != group)  {
  71.       fprintf(stderr, "lattice rule %s used in different groups\n",REPR(mem));
  72.       exit(1);
  73.     }
  74.   }
  75.   NODENAME(ag) = 0;
  76.   for (mem = SON(ag) ; mem != nil; mem = BROTHER(mem)) 
  77.     if (DEF(mem) == -1) 
  78.       NODENAME(ag) |= NODENAME(mem);
  79.     else 
  80.       NODENAME(ag) |= NODENAME(DEF(mem));
  81.  
  82. }
  83.  
  84. set_mem_lattice(mem)
  85. {
  86.    register int    ag,member;
  87.    register char * repr = REPR(mem);
  88.    for (ag = top;  ag != nil; ag = BROTHER(ag))
  89.       for (member = SON(ag); member != nil; member = BROTHER(member))  
  90.           if (mem == member){
  91.              NODENAME(mem) =  1 << count++;
  92.              if (count > 30) {
  93.                fprintf(stderr, "lattice rule %s: to much elements (>31)",
  94.                             REPR(ag));
  95.                exit(1);
  96.              }
  97.              return;
  98.            } else if (REPR(member) == repr) {
  99.               NODENAME(mem) = NODENAME(member);
  100.               return;
  101.            }
  102. }               
  103. link_lattice()
  104. {
  105.    register int    ag,member;
  106.  
  107.    for (ag = first_lattice; ag != nil; ag = BROTHER(ag))
  108.       for (member = SON(ag); member != nil; member = BROTHER(member))  {
  109.          register int r_ag = first_lattice;
  110.          while (REPR(member) != REPR(r_ag)) {
  111.             if (r_ag == nil)  {
  112.                DEF(member) = -1;
  113.                break;
  114.             } else
  115.                r_ag = BROTHER(r_ag);
  116.          }
  117.          if (r_ag != nil)  
  118.                DEF(member) = r_ag;
  119.       }
  120.    set_lattice_groups();
  121. }
  122.  
  123.  
  124. lattice_defined(rule,alt,trm)
  125. int alt,trm;
  126. {
  127.    register int    ag,l;
  128.    for (ag = first_lattice; ag != nil; ag = BROTHER(ag))
  129.       if (REPR(trm) == REPR(ag))  {
  130.          LATTICE_DEF(trm) = ag;
  131.          return;
  132.       }
  133.    l = LINE(alt);
  134.    if (separate_comp_flag)
  135.       fprintf(stderr,"In %s:\n",PART(rule));
  136.    if (first_lattice != nil)  {
  137.       fprintf(stderr,"line %d: LATTICE affix %s not defined\n",l,REPR(trm));
  138.       fprintf(stderr,"line %d, affix %s: FLOW SYMBOL expected\n",l,REPR(trm));
  139.    } else  
  140.       fprintf(stderr,"line %d,affix %s: FLOW SYMBOL expected\n",l,REPR(trm));
  141.    exit(1);
  142. }
  143.  
  144.  
  145. tr_lattice() {
  146.   register int ag,mem, afx,alt,rule;
  147.   if (lat_trad_flag)  {
  148.        transformlattice = tltraditional;
  149.        transformlatticeterm = tltraditionalterm;
  150.   }
  151.   for (rule = root; rule != laststdpred; rule = BROTHER(rule)) {
  152.     lline = LINE(SON(rule));
  153.     for (alt = SON (rule) ; alt != nil; alt = BROTHER (alt)) {
  154.       for (afx = AFFIXDEF (alt) ; afx != nil; afx = BROTHER (afx))
  155.         if (LATTICE(afx) ) 
  156.           if (defining_occurence_of_lattice_affix(REPR(SON(afx)),SON(alt)))
  157.              tr_lattice_to_afx(SON(afx),alt,SON(alt));
  158.           else 
  159.              tr_lattice_term_to_afx(SON(afx),alt,SON(alt));
  160.       for (mem = SON (alt) ; mem != nil; mem = BROTHER (mem))
  161.        if (DEF(mem) != transformlattice)
  162.         for (afx = AFFIXTREE (mem) ; afx != nil; afx = BROTHER (afx))
  163.               if (LATTICE(afx) ) 
  164.                 tr_lattice_to_afx(SON(afx),alt,mem);
  165.     }
  166.   }
  167.  if (lat_error>0)  exit(1);
  168. }
  169. int defining_occurence_of_lattice_affix(repr,mem) 
  170. char * repr;
  171. int mem;
  172. {    
  173.       int afx;
  174.       for (; mem != nil; mem = BROTHER (mem))
  175.         for (afx = AFFIXTREE (mem) ; afx != nil; afx = BROTHER (afx))
  176.            if ((LATTICE(afx)) && (repr == REPR(SON(afx))))
  177.                return true;
  178.       return false;
  179.  
  180. int done;
  181.  
  182. tr_lattice_term_to_afx(trm,alt,member)
  183. int trm,alt,member;
  184. {
  185.       register int afx,mem, term;
  186.       register char * repr = REPR(LATTICE_DEF(trm));
  187.       int lat_val = NODENAME(LATTICE_DEF(trm));
  188.       
  189.  
  190.       group = DEF(LATTICE_DEF(trm)); 
  191.       lattice_substitution_name = &chartable[++charindex];
  192.       (void) sprintf(&chartable[charindex], "T_%d", unique_lattice_count++);
  193.       charindex += 10;
  194.       if (charindex > maxchars) 
  195.            alloc_chartable();
  196.       lattice_val_repr = &chartable[++charindex];
  197.       (void) sprintf(&chartable[charindex], "%d", lat_val);
  198.       charindex += 20;
  199.       if (charindex > maxchars) 
  200.            alloc_chartable();
  201.  
  202.       done = false;
  203.       for (afx = AFFIXDEF (alt) ; afx != nil; afx = BROTHER (afx))
  204.         if (!LATTICE(afx) ) 
  205.           for (term = SON (afx); term != nil; term = BROTHER (term))
  206.              if (repr == REPR(term)) {
  207.                if (!done) {
  208.                   done = true;
  209.                   add_lattice_term_to_afx_node(term,alt);
  210.                }
  211.                else 
  212.                  REPR(term) = lattice_substitution_name; 
  213.              }
  214.       prev_mem = nil;
  215.       for (mem = SON (alt);mem != nil; prev_mem = mem,mem = BROTHER (mem))
  216.        if (DEF(mem) != transformlattice)
  217.         for (afx = AFFIXTREE (mem) ; afx != nil; afx = BROTHER (afx))
  218.           if (!LATTICE(afx) )
  219.             for (term = SON (afx); term != nil; term = BROTHER (term))
  220.              if (repr == REPR(term)) {
  221.                if (!done) {
  222.                   done = true;
  223.                   insert_lattice_term_to_afx_node(term,mem,alt);
  224.                   check_used_before_defined(mem,member,alt,repr); 
  225.                }
  226.                else 
  227.                  REPR(term) = lattice_substitution_name;
  228.              }
  229. }
  230.  
  231. tr_lattice_to_afx(trm,alt,member)
  232. int trm,alt,member;
  233. {
  234.       register int afx,mem, term;
  235.       register char * repr = REPR(LATTICE_DEF(trm));
  236.  
  237.       group = DEF(LATTICE_DEF(trm)); 
  238.       lattice_substitution_name = &chartable[++charindex];
  239.       (void) sprintf(&chartable[charindex], "T_%d", unique_lattice_count++);
  240.       charindex += 10;
  241.       if (charindex > maxchars) 
  242.            alloc_chartable();
  243.       done = false;
  244.       for (afx = AFFIXDEF (alt) ; afx != nil; afx = BROTHER (afx))
  245.         if (!LATTICE(afx) ) 
  246.           for (term = SON (afx); term != nil; term = BROTHER (term))
  247.              if (repr == REPR(term)) {
  248.                if (!done) {
  249.                   done = true;
  250.                   add_lattice_to_afx_node(repr,term,alt);
  251.                }
  252.                else 
  253.                  REPR(term) = lattice_substitution_name; 
  254.              }
  255.       prev_mem = nil;
  256.       for (mem = SON (alt);mem != nil; prev_mem = mem,mem = BROTHER (mem))
  257.        if (DEF(mem) != transformlattice)
  258.         for (afx = AFFIXTREE (mem) ; afx != nil; afx = BROTHER (afx))
  259.           if (!LATTICE(afx) )
  260.             for (term = SON (afx); term != nil; term = BROTHER (term))
  261.              if (repr == REPR(term)) {
  262.                if (!done) {
  263.                   done = true;
  264.                   if (DERIVED(afx))
  265.                      append_lattice_to_afx_node(trm,term,mem);
  266.                   else    
  267.                      insert_lattice_to_afx_node(trm,term,mem,alt);
  268.                   check_used_before_defined(mem,member,alt,repr); 
  269.                }
  270.                else 
  271.                  REPR(term) = lattice_substitution_name;
  272.              }
  273. }
  274.  
  275. check_used_before_defined(use_mem,def_mem,alt,repr) 
  276. int use_mem,def_mem,alt;
  277. char *repr;
  278. {
  279.  int mem ;
  280.  for (mem = SON(alt); (mem != nil) && (def_mem != mem); mem = BROTHER(mem)) 
  281.    if (use_mem == mem) {
  282.      fprintf(stderr, 
  283. "line %d in %s: cannot delay evaluation of lattice term '%s' (defined at '%s')\n",
  284.         lline,REPR(use_mem),repr,REPR(def_mem));
  285.       lat_error +=1;
  286.    }
  287. }
  288. /*  
  289.  *  add node "transform  lattice (>group,>LATTICE, T_%d.>).
  290.  */
  291.  
  292. add_lattice_to_afx_node(lattice,term,alt)
  293. int  term, alt;
  294. char *lattice;
  295. {
  296.    int b,mem ;
  297. /*  T_%d> */
  298.    REPR(term) = lattice_substitution_name;
  299.    newnode(affixnt, nil, nil, lattice_substitution_name);
  300.    newnode(derived, nil, brother, "(nil)");
  301.    b = brother;
  302. /*  LATTICE */
  303.    newnode(affixnt, nil, nil, lattice);
  304.    newnode(inherited, b,brother, "(nil)");
  305.    b = brother;
  306. /*  >"groupnr" */
  307.    newnode(affixtm,  nil, nil, repr_group[group]);
  308.    newnode(inherited, b, brother, "(nil)");
  309.  
  310. /*  transform lattice */
  311.    newdefnode(ntnode, nil, brother, transformlattice, 
  312.                    REPR(transformlattice));
  313.  
  314.  mem = SON(alt);
  315.  if (mem  == nil) {
  316.       SON(alt) = brother;
  317.       return;
  318.  }
  319.  for  (; BROTHER(mem) != nil; mem = BROTHER(mem));
  320.  BROTHER (mem) = brother;
  321. }
  322.  
  323. /*  
  324.  *  append  node "transform  lattice (>group,LATTICE, T_%d.>).
  325.      after mem
  326.  */
  327.  
  328. append_lattice_to_afx_node(lattice,term,mem)
  329. int lattice,term, mem;
  330. {
  331.    int b ;
  332.    REPR(term) = lattice_substitution_name;
  333.  
  334. /*  T_%d> */
  335.    newnode(affixnt, nil, nil, lattice_substitution_name);
  336.    newnode(derived, nil, brother, "(nil)");
  337.    b = brother;
  338. /*  LATTICE */
  339.    newnode(affixnt, nil, nil, REPR(lattice));
  340.    newnode(inherited, b,brother, "(nil)");
  341.    b = brother;
  342. /*  >"groupnr" */
  343.    newnode(affixtm,  nil, nil, repr_group[group]);
  344.    newnode(inherited, b, brother, "(nil)");
  345.  
  346. /*  transform lattice */
  347.    newdefnode(ntnode, nil, brother, transformlattice, 
  348.                    REPR(transformlattice));
  349.    b = BROTHER(mem);
  350.    BROTHER (mem) = brother;
  351.    BROTHER (brother) = b;
  352.  
  353. }
  354.  
  355. insert_lattice_to_afx_node(lattice,term,mem,alt)
  356. int lattice,term, mem;
  357. {
  358.    int b ;
  359.    REPR(term) = lattice_substitution_name;
  360.  
  361. /*  T_%d> */
  362.    newnode(affixnt, nil, nil, lattice_substitution_name);
  363.    newnode(derived, nil, brother, "(nil)");
  364.    b = brother;
  365. /*  LATTICE */
  366.    newnode(affixnt, nil, nil, REPR(lattice));
  367.    newnode(inherited, b,brother, "(nil)");
  368.    b = brother;
  369. /*  >"groupnr" */
  370.    newnode(affixtm,  nil, nil, repr_group[group]);
  371.    newnode(inherited, b, brother, "(nil)");
  372.  
  373. /*  transform lattice */
  374.    newdefnode(ntnode, mem, brother, transformlattice, 
  375.                    REPR(transformlattice));
  376.    if (prev_mem == nil) 
  377.       SON(alt) = brother;
  378.    else  
  379.       BROTHER(prev_mem) = brother;
  380.    prev_mem = brother;   
  381. }
  382.  
  383. /*  
  384.  *  add node "transform  lattice (>group,>"valLATTICE", T_%d.>).
  385.  */
  386.  
  387. add_lattice_term_to_afx_node(term,alt)
  388. int  term, alt;
  389. {
  390.    int b,mem ;
  391. /*  T_%d> */
  392.    REPR(term) = lattice_substitution_name;
  393.    newnode(affixnt, nil, nil, lattice_substitution_name);
  394.    newnode(derived, nil, brother, "(nil)");
  395.    b = brother;
  396. /*  LATTICE */
  397.    newnode(affixtm, nil, nil, lattice_val_repr);
  398.    newnode(inherited, b,brother, "(nil)");
  399.    b = brother;
  400. /*  >"groupnr" */
  401.    newnode(affixtm,  nil, nil, repr_group[group]);
  402.    newnode(inherited, b, brother, "(nil)");
  403.  
  404. /*  transform lattice */
  405.    newdefnode(ntnode, nil, brother, transformlatticeterm, 
  406.                    REPR(transformlatticeterm));
  407.  
  408.  mem = SON(alt);
  409.  if (mem  == nil) {
  410.       SON(alt) = brother;
  411.       return;
  412.  }
  413.  for  (; BROTHER(mem) != nil; mem = BROTHER(mem));
  414.  BROTHER (mem) = brother;
  415. }
  416.  
  417. /*  
  418.  *  insert  node "transform  lattice (>group,>"valLATTICE", T_%d.>).
  419.      after mem
  420.  */
  421.  
  422. insert_lattice_term_to_afx_node(term,mem,alt)
  423. int term, mem;
  424. {
  425.    int b ;
  426.    REPR(term) = lattice_substitution_name;
  427.  
  428. /*  T_%d> */
  429.    newnode(affixnt, nil, nil, lattice_substitution_name);
  430.    newnode(derived, nil, brother, "(nil)");
  431.    b = brother;
  432. /*  LATTICE */
  433.    newnode(affixtm, nil, nil, lattice_val_repr);
  434.    newnode(inherited, b,brother, "(nil)");
  435.    b = brother;
  436. /*  >"groupnr" */
  437.    newnode(affixtm,  nil, nil, repr_group[group]);
  438.    newnode(inherited, b, brother, "(nil)");
  439.  
  440. /*  transform lattice */
  441.    newdefnode(ntnode, mem, brother, transformlatticeterm, 
  442.                    REPR(transformlatticeterm));
  443.    if (prev_mem == nil) 
  444.       SON(alt) = brother;
  445.    else  
  446.       BROTHER(prev_mem) = brother;
  447.    prev_mem = brother;   
  448. }
  449.  
  450.  
  451. /* code part */
  452.  
  453. int    el_count; 
  454. conv_table() {
  455.    int ag ;
  456.    if (!MARKED(root,docompile)) {
  457.       fprintf(output,"struct char_ptr_list { char *l[32];};\n"); 
  458.       fprintf(output,"extern struct char_ptr_list groups[%d];\n",max_group); 
  459.       return;
  460.    }
  461.    group = -1;
  462.    fprintf(output,"struct char_ptr_list { char *l[32];};\n"); 
  463.    fprintf(output,"struct char_ptr_list groups[%d] = {\n",max_group); 
  464.    for ( ag = first_lattice ; ag != nil; ag = BROTHER(ag))
  465.      if (top_def(ag))  { 
  466.         fprintf(output,"{"); 
  467.         group  = DEF(ag);
  468.         top = ag;
  469.         count = 0;
  470.         el_count = 1;
  471.         code_group(ag);
  472.         if (el_count-1 != NODENAME(ag) )
  473.            fprintf(stderr,"glammar : lattice code generation error?\
  474.            ag = %x, el_count =%x\n",NODENAME(ag),el_count); 
  475.              
  476.         fprintf(output,"},\n"); 
  477.       }
  478.    fprintf(output,"};\n"); 
  479. }
  480.  
  481. int top_def(ag)  
  482. int ag;
  483. {
  484.   int g;
  485.   
  486.   for ( g = first_lattice; g != nil; g = BROTHER(g))
  487.      if (DEF(g) == DEF(ag))
  488.           return g == ag;
  489.   return false;
  490. }
  491. code_group(ag)
  492. int ag;
  493. {
  494.   int mem;
  495.   for (mem = SON(ag) ; mem != nil; mem = BROTHER(mem)) {
  496.     int def  = DEF(mem);
  497.     if ( def == -1) {
  498.       if  (el_count == NODENAME(mem))  {
  499.        el_count <<= 1;
  500.        code_lattice_el(mem);
  501.       }
  502.     }
  503.     else   
  504.       code_group(def);
  505.    }
  506. }
  507.  
  508.  
  509. code_lattice_el(mem)
  510. {
  511.    register int    ag,member;
  512.    register char * repr = REPR(mem);
  513.    for (ag = top;  ag != nil; ag = BROTHER(ag))
  514.       for (member = SON(ag); member != nil; member = BROTHER(member))  
  515.           if (mem == member) {
  516.              fprintf(output,"\"%s\",\n\t",repr);
  517.              return;
  518.            } else if (REPR(member) == repr)  {
  519.               fprintf(output,"\"%s\",\n\t",repr);
  520.               return;
  521.            }
  522.            
  523. }
  524.  
  525. /* 
  526.  * Need to know if a lattice term occuring in the lefthandside
  527.  * is used in the righthandside.
  528.  * If it is then we can use the name of the term;
  529.  * otherwise a terminal is made with its value converted 
  530.  * to decimal as the terminals value.
  531.  * Here we only make it a terminal; at code generation 
  532.  * the value of the lattice-term is known and filled in.
  533.  */
  534.  
  535. lattice_used(term, alt)
  536. int             term, alt;
  537. {
  538.    int             affix, mem, trm;
  539.  
  540.    for (mem = SON(alt); mem != nil; mem = BROTHER(mem)) {
  541.       for (affix = AFFIXTREE(mem); affix != nil; affix = BROTHER(affix))
  542.          if (LATTICE(affix)) {
  543.             trm = SON(affix);
  544.             if (REPR(trm) == REPR(term))
  545.                return;
  546.         }
  547.    }
  548.    NODENAME(term) = affixtm;
  549. }
  550.  
  551. int lattice_top(afx)
  552. {
  553.    register int    ag,def,n,r;
  554.    def = LATTICE_DEF(SON(afx));
  555.    n = DEF (def);
  556.    r =  NODENAME(def);
  557.    for (ag = first_lattice;  ag != nil; ag = BROTHER(ag))
  558.           if (DEF(ag) == n)
  559.             return NODENAME(ag) == r;
  560.    return false;
  561. }
  562.